<!--
 * @Author: weidewei
 * @Date: 2025-11-18 13:45:40
 * @LastEditors: weidewei
 * @LastEditTime: 2025-11-18 14:33:06
 * @Description: 
 * @FilePath: \2025-front-face\review-knowledge\20251118\复习数字千位分隔符.html
-->
<script>
    // function numberThousandSeparator(num) {
    //      return num.toLocaleString();       
    // }
    // console.log(numberThousandSeparator(1000.1234));


    function numberThousandSeparator(num) {
      let intNum = Math.floor(num).toString();
      let result = '';
      let count = 0;
      for(let i=intNum.length-1;i>=0;i--) {
        count++;
        result = intNum[i] + result;
        if(count % 3 === 0 && i !== 0) {
            result = ',' + result;
        }
      }
    //   console.log(result)
      const decimalNum = num.toString().split('.')[1];
      if(num % 1 !== 0) {
        result = result + '.' + decimalNum;
      }
      return result;
    }

    console.log(numberThousandSeparator(1000.22))
</script>